package cn.cocho.android.mct.comp.dialog;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import cn.cocho.android.mc.comp.dialog.McProgressBar;
import cn.cocho.android.mct.R;
import cn.cocho.android.mc.activity.ActivityUtils;
import cn.cocho.android.mc.async.ProgressAsyncTask;
import cn.cocho.android.mc.comp.dialog.LoadingDialog;
import cn.cocho.android.mc.comp.dialog.ProgressDialog;
import cn.cocho.android.mc.comp.dialog.McDialog;

public class McDialogTest extends Activity {
    boolean flag = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mc_dialog_test);
        initLoadingDialog();
        initDialog();
        initProgressDailog();
        initProgressBar();
        initProgressAsyncTask();
    }

    private void initLoadingDialog() {
        Button btn = (Button) findViewById(R.id.load_dialog);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                LoadingDialog dialog = new LoadingDialog(McDialogTest.this, "加载中...");
                // 设置dialog 的进度条是否不明确
                dialog.setIndeterminate(false);
                // 设置dialog 是否可以按退回按键取消
                dialog.setCancelable(true);
                dialog.show();

            }
        });
    }

    private void initDialog() {
        Button btn_ok = (Button) findViewById(R.id.dialog_ok_cancel);
        btn_ok.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                final McDialog dialog = new McDialog(McDialogTest.this);
                dialog.setTitle("提醒");
                dialog.setMessage("当前3G网络，观看课程或者下载课程，可能会耗费大量的流量，确定继续吗");
                dialog.setPositiveButton("我知道了", new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(McDialogTest.this, "确定了啊",
                                Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    }
                });
                // dialog.setNegativeButton("取消", new OnClickListener() {
                // @Override
                // public void onClick(View v) {
                // Toast.makeText(DialogTest.this, "取消",
                // Toast.LENGTH_SHORT).show();
                // dialog.dismiss();
                // }
                // });
                dialog.show();
            }
        });
    }


    private void initProgressDailog() {
        Button flatD = (Button) findViewById(R.id.flat_progress_btn);
        flatD.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                final ProgressDialog dialog = new ProgressDialog();
                dialog.setTitle("提醒");
                dialog.setMessage("正在加载数据，请稍候...");
                // 设置dialog 是否可以按退回按键取消
                dialog.setCancelable(false);
                dialog.getCancelButton().setOnClickListener(
                        new OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                dialog.dismiss();
                                ActivityUtils.showLongMessage("关闭对话框");
                            }
                        });
                dialog.show();

            }
        });
    }

    private void initProgressBar() {
        Button flatD = (Button) findViewById(R.id.mc_progress_bar_btn);
        flatD.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                final McProgressBar mDialog = new McProgressBar(McDialogTest.this);
                mDialog.setMessage("同步数据中...");
                mDialog.setCancelable(false);
                mDialog.setCancelBtnListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mDialog.dismiss();
                    }
                });
            }
        });

    }

    private void initProgressAsyncTask() {
        Button flatD = (Button) findViewById(R.id.progress_async_task_btn);
        flatD.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                new ProgressAsyncTask<Integer, Object, Boolean>(McDialogTest.this, "数据同步中，请稍候...") {
                    @Override
                    protected Boolean doInBackground(Integer... arg0) {
                        try {
                            Thread.sleep(10000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                            return false;
                        }
                        return true;
                    }

                    @Override
                    protected void onPostExecute(Boolean result) {
                        super.onPostExecute(result);
                    }
                }.execute();
            }
        });

    }
}